import 'package:dio/dio.dart';
import 'package:flutter_base/http/base_model.dart';
import 'package:oktoast/oktoast.dart';

///
/// @DIR_PATH：lib/http
/// @TIME：2024/6/6 21:03
/// @AUTHOR：starr
///
///
class ResponseInterceptor extends InterceptorsWrapper{

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if(response.statusCode==200){

      //单独处理检查升级的接口
      if(response.requestOptions.path.contains("apiv2/app/check")){
        handler.next(response);
      }else{
        try{
          var rsp = BaseModel.fromJson(response.data);
          if(rsp.errorCode==0){
            if(rsp.data==null){
              //解析成功，但是没有数据
              handler.next(Response(requestOptions: response.requestOptions,data: true));
            }else{
              //解析成功，并且有数据
              handler.next(Response(requestOptions: response.requestOptions,data: rsp.data));
            }
            //未登录
          }else if(rsp.errorCode == -1001) {
            handler.reject(DioException(requestOptions: response.requestOptions,message: "未登录"));
            showToast("未登录");
          } else if (rsp.errorCode == -1) {
            showToast("${rsp.errorMsg}");
          } else {
            //未知错误
            handler.reject(DioException(requestOptions: response.requestOptions,message: "未知错误"));
            showToast("未知错误");
          }
        }catch (e){
          handler.reject(DioException(requestOptions: response.requestOptions,message: "$e"));
        }
      }
    }else{
      handler.reject(DioException(requestOptions: response.requestOptions));
    }
  }
}